package logic

import (
	"MyBlogv2/Servers/Article/article"
	"MyBlogv2/Servers/Article/internal/svc"
	define "MyBlogv2/blog-common/define"
	"MyBlogv2/blog-common/helper"
	"MyBlogv2/blog-common/models"
	"context"
	"github.com/jinzhu/copier"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetRecommendListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetRecommendListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRecommendListLogic {
	return &GetRecommendListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetRecommendListLogic) GetRecommendList(in *Article.ArticleRequest) (*Article.RecommendArticleVoList, error) {
	var articleVoList []models.RecommendArticleVo
	var articleList []models.Article
	models.Db.Model(new(models.Article)).
		Where("is_top = ?", define.RECOMMEND_ARTICLE).
		Where("status = ?", define.PUBLIC_ARTICLE).
		Find(&articleList)
	copier.Copy(&articleVoList, &articleList)
	var recommendArticleVoList []*Article.RecommendArticleVo
	//copier.Copy(&result, &articleVoList)
	for _, v := range articleVoList {
		var recommendArticleVo Article.RecommendArticleVo
		copier.Copy(&recommendArticleVo, &v)
		recommendArticleVo.CreateTime = helper.MyTimeToTimestamp(v.CreateTime)
		recommendArticleVoList = append(recommendArticleVoList, &recommendArticleVo)
	}

	return &Article.RecommendArticleVoList{
		RecommendArticleVoList: recommendArticleVoList,
	}, nil
}
